package db2;

import beans2.Doctor;

import java.sql.*;

public class DoctorDao  extends Connbean{
    private Connection connection=null;
    public Doctor getRecord(String id){
        ResultSet rs=null;
        Statement stmt=null;
        Doctor doctor=null;
        try {
            connection = getConnection();
            if(connection==null)
                System.out.println("error");
            String sql="select * from YY_doctor where id='"+id+"'";
            stmt =connection.createStatement();
            rs=stmt.executeQuery(sql);
            if(rs!=null && rs.next()){
                doctor=new Doctor();
                doctor.setId(rs.getString(1));
                doctor.setPassword(rs.getString(2));
                doctor.setName(rs.getString(3));
                doctor.setIdNumber(rs.getString(4));
                doctor.setSex(rs.getString(5));
                doctor.setKsid(rs.getString(6));
                doctor.setPhoto(rs.getString(7));
                doctor.setIntroduction(rs.getString(8));
                doctor.setType(rs.getString(9));
            }
            else
            {
                System.out.println("null");
                doctor=null;
            }
            stmt.close();
            connection.close();
        }catch (SQLException e) {
            e.printStackTrace();
        } finally{
            try{
                if (connection!=null && (!connection.isClosed())){
                    connection.close();
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
        return doctor;
    }

    public boolean updateRecord(Doctor doctor)
    {
        PreparedStatement pstmt=null;
        String sql="update YY_doctor set id=?,password=?,name=?,idNumber=?,sex=?,ksid=?,photo=? ,introduction=?,type=?"+"where id='"+doctor.getId()+"'";
        int result=0;
        try{
            connection=getConnection();
            pstmt=connection.prepareStatement(sql);
            pstmt.setString(1,doctor.getId());
            pstmt.setString(2,doctor.getPassword());
            pstmt.setString(3,doctor.getName());
            pstmt.setString(4,doctor.getIdNumber());
            pstmt.setString(5,doctor.getSex());
            pstmt.setString(6,doctor.getKsid());
            pstmt.setString(7,doctor.getPhoto());
            pstmt.setString(8,doctor.getIntroduction());
            pstmt.setString(9,doctor.getType());

            result=pstmt.executeUpdate();
            pstmt.close();
            connection.close();
        }catch (SQLException e) {
            e.printStackTrace();
        } finally{
            try{
                if (connection!=null && (!connection.isClosed())){
                    connection.close();
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
        return result>0?true:false;
    }
}
